package com.jn.gsondemo.views;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.jn.gsondemo.R;

import org.json.JSONException;
import org.json.JSONObject;

public class BookDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);

        // 假设你已经从某个地方获取了book.json的字符串
        String bookJsonString = getIntent().getStringExtra("book_json");

        try {
            JSONObject bookJson = new JSONObject(bookJsonString);
            JSONObject result = bookJson.getJSONObject("result");
            JSONObject data = result.getJSONObject("data");

            String imgUrl = data.getString("img");
            Log.d("BookDetailActivity", "Image URL: " + imgUrl);

            ImageView bookImage = findViewById(R.id.book_image);
            TextView bookTitle = findViewById(R.id.book_title);
            TextView bookCatalog = findViewById(R.id.book_catalog);
            TextView bookTags = findViewById(R.id.book_tags);
            TextView bookSub1 = findViewById(R.id.book_sub1);
            TextView bookSub2 = findViewById(R.id.book_sub2);
            TextView bookReading = findViewById(R.id.book_reading);
            TextView bookOnline = findViewById(R.id.book_online);
            TextView bookBytime = findViewById(R.id.book_bytime);

            Glide.with(this)
                    .load(imgUrl)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            Log.e("BookDetailActivity", "Glide load failed: " + e.getMessage());
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            Log.d("BookDetailActivity", "Glide load success");
                            return false;
                        }
                    })
                    .into(bookImage);

            bookTitle.setText(data.getString("title"));
            bookCatalog.setText("分类: " + data.getString("catalog"));
            bookTags.setText("标签: " + data.getString("tags"));
            bookSub1.setText(data.getString("sub1"));
            bookSub2.setText(data.getString("sub2"));
            bookReading.setText("阅读量: " + data.getString("reading"));
            bookOnline.setText("在线购买: " + data.getString("online"));
            bookBytime.setText("发布时间: " + data.getString("bytime"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
